package com.css.fxfzxczh.modules.lossAssessment.resetPrice.repository.impl;

import com.css.fxfzxczh.modules.lossAssessment.resetPrice.entity.ResetPrice;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.repository.ResetPriceRepository;
import com.css.fxfzxczh.modules.lossAssessment.resetPrice.vo.ResetPriceVO;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/22
 * @Description: 设定重置单价
 */
@Repository
public class ResetPriceRepositoryImpl implements ResetPriceRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static final String TABLE_NAME = "xczh_reset_price"; // 表名
    private static final String TABLE_ID = "id"; // 表id字段名


    @Override
    public Map<String, Object> getPage(int curPage, int pageSize, String xsemId) {
        StringBuilder sql = new StringBuilder("select a.*,b.comment_info from " + TABLE_NAME + " a " +
                "left join xczh_house_structure_type_management b on a.structure_type=b.structure_type");
        StringBuilder whereSql = new StringBuilder(" where a.del_flag = '0' and b.del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time asc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        //地震事件id
        if (!PlatformObjectUtils.isEmpty(xsemId)) {
            whereSql.append(" and xsem_id = '" + xsemId + "'");
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ResetPrice.class)).size();
        List<ResetPriceVO> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(ResetPriceVO.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public void batchSave(ResetPrice resetPrices) {
        String sql = "INSERT INTO "+TABLE_NAME+" (id,xsem_id,structure_type," +
                "reset_price,reporter,reporter_time,del_flag,create_user,create_time)" +
                "values (:id,:xsemId,:structureType," +
                ":resetPrice,:reporter,:reporterTime,:delFlag,:createUser,:createTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(resetPrices);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void deleteById(List<String> ids) {
        String sql = "update " + TABLE_NAME + " set del_flag = '1'  where " + TABLE_ID + " in (:ids)";
        Map idMap = new HashMap();
        idMap.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, idMap);
    }

    @Override
    public void updateById(ResetPrice param) {
        String sql = "update " + TABLE_NAME +
                " set reset_price = :resetPrice,update_user = :updateUser,update_time = :updateTime " +
                "where del_flag = '0' and " + TABLE_ID + " = '"+param.getId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(param);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public ResetPrice findById(String id) {
        String sql = "select * from " + TABLE_NAME + " where " + TABLE_ID + " = '" + id + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ResetPrice.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public ResetPrice queryTypeByXsemId(String s, String xsemId) {
        String sql = "select * from " + TABLE_NAME + " where del_flag = '0' and xsem_id = '" + xsemId + "' and structure_type = '"+s+"'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ResetPrice.class));
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public List<ResetPrice> getResetPriceByXsemId(String xsemId) {
        StringBuilder sql = new StringBuilder("select * from " + TABLE_NAME +" where del_flag = '0' and xsem_id = '" + xsemId + "' order by structure_type");
        List<ResetPrice> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(ResetPrice.class));
        return list;
    }
}
